package com.zide.platform.controller;

import com.zide.auth.domain.entity.platform.dept.PlatformDept;
import com.zide.common.constant.UserConstants;
import com.zide.common.core.domain.CommonResult;
import com.zide.common.enums.BusinessType;
import com.zide.common.utils.StringUtils;
import com.zide.platform.service.PlatformDeptService;
import com.zide.platform.vo.result.dept.PlatformDeptResultVO;
import com.zide.platform.vo.search.dept.PlatformDeptSearchVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 */
@Tag(name = "后台管理 - 部门信息管理")
@RestController
@RequestMapping("/platform/dept")
@RequiredArgsConstructor
public class PlatformDeptController {
    
    private final PlatformDeptService deptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public CommonResult list(PlatformDeptSearchVO dept) {
        return CommonResult.success(deptService.selectDeptList(dept));
    }

    /**
     * 查询部门列表（排除节点）
     */
    @GetMapping("/list/exclude/{deptId}")
    public CommonResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<PlatformDeptResultVO> depts = deptService.selectDeptList(new PlatformDeptSearchVO());
        depts.removeIf(d -> d.getId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return CommonResult.success(depts);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @GetMapping(value = "/{deptId}")
    public CommonResult getInfo(@PathVariable Long deptId) {
        deptService.checkDeptDataScope(deptId);
        return CommonResult.success(deptService.selectDeptById(deptId));
    }

    /**
     * 新增部门
     */
    @PostMapping
    public CommonResult add(@Validated @RequestBody PlatformDept dept) {
        if (!deptService.checkDeptNameUnique(dept)) {
            return CommonResult.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        return CommonResult.success(deptService.insertDept(dept));
    }

    /**
     * 修改部门
     */
    @PutMapping
    public CommonResult edit(@Validated @RequestBody PlatformDept dept) {
        Long deptId = dept.getId();
        deptService.checkDeptDataScope(deptId);
        if (!deptService.checkDeptNameUnique(dept)) {
            return CommonResult.error("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        } else if (dept.getParentId().equals(deptId)) {
            return CommonResult.error("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        } else if (UserConstants.DEPT_DISABLE.equals(dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
            return CommonResult.error("该部门包含未停用的子部门！");
        }
        return CommonResult.success(deptService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{deptId}")
    public CommonResult remove(@PathVariable Long deptId) {
        if (deptService.hasChildByDeptId(deptId)) {
            return CommonResult.warn("存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId)) {
            return CommonResult.warn("部门存在用户,不允许删除");
        }
        deptService.checkDeptDataScope(deptId);
        return CommonResult.success(deptService.deleteDeptById(deptId));
    }
}
